package io.github.uicdb.liba.api.register;

import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.registries.RegistryObject;

import javax.annotation.Nullable;
import java.util.Objects;
import java.util.function.Supplier;

public final class BlockEntityTypeHolder<T extends BlockEntity> implements Supplier<BlockEntityType<T>> {
    public RegistryObject<BlockEntityType<T>> type;


    @Override
    public BlockEntityType<T> get() {
        return type.get();
    }

    @Nullable
    public T create(BlockPos pPos, BlockState pState) {
        if (type != null)
            return get().create(pPos, pState);
        return null;
    }

    public RegistryObject<BlockEntityType<T>> type() {
        return type;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) return true;
        if (obj == null || obj.getClass() != this.getClass()) return false;
        var that = (BlockEntityTypeHolder) obj;
        return Objects.equals(this.type, that.type);
    }

    @Override
    public int hashCode() {
        return Objects.hash(type);
    }

    @Override
    public String toString() {
        return "BlockEntityTypeHolder[" +
                "type=" + type + ']';
    }

    @FunctionalInterface
    public interface BESupplier<T extends BlockEntity> {
        T create(BlockEntityType<T> type, BlockPos pPos, BlockState pState);
    }
}
